<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="所属任务id" prop="taskId" label-width="90px">
        <el-input style="width: 130px" v-model="queryParams.taskId" placeholder="请输入任务id" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="任务名称" prop="taskName">
        <el-input v-model="queryParams.taskName" placeholder="请输入任务名称" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="用户id" prop="customerId">
        <el-input style="width: 130px" v-model="queryParams.customerId" placeholder="请输入用户id" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="用户提交内容" prop="submitContext" label-width="120px">
        <el-input v-model="queryParams.submitContext" style="width: 333px;" placeholder="请输入用户提交内容" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="订单状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择订单状态" clearable size="small">
          <el-option v-for="dict in statusOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="warning" icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['business:order:export']" >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单id" align="center" prop="id" />
      <el-table-column label="任务id" align="center" prop="taskId" />
      <el-table-column label="任务名称" align="center" prop="taskName" />
      <el-table-column label="用户id" align="center" prop="customerId" />
      <el-table-column label="用户提交内容" align="center" prop="submitContext" width="600" class-name="order-status-css-orangered" />
      <el-table-column label="任务价格" align="center" prop="price" />
      <el-table-column label="订单状态" align="center" prop="status" :formatter="statusFormat" width="200" >
        <template slot-scope="scope">
          <span :class="orderStatusCssMethod(scope.row.status)">{{statusOptions[scope.row.status].dictLabel}}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" header-align="center" align="right" class-name="small-padding fixed-width" fixed="right" width="120">
        <template slot-scope="scope">
          <el-button v-if="scope.row.status === 1" size="mini" type="text" icon="el-icon-edit" @click="checkTask(scope.row.id)">审核</el-button>
          <el-button v-if="scope.row.status === 2" size="mini" type="text" icon="el-icon-edit" @click="backCheckTask(scope.row.id)">撤回</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit-outline" >查看</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!--  直接审核弹出层  -->
    <el-dialog :title="title" :visible.sync="checkTaskDialog" width="400px" append-to-body>
      <el-form ref="checkTaskForm" :model="checkTaskForm">
        <el-form-item>
          <el-input type="hidden" v-model="checkTaskForm.taskLogId"/>
        </el-form-item>
        <el-form-item label="审核方式">
          <el-radio-group v-model="checkTaskForm.status">
            <el-radio label="2">审核通过</el-radio>
            <el-radio label="3">审核不通过</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="不通过理由, 不通过时需要填写">
          <el-input type="textarea" v-model="checkTaskForm.msg"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="checkTaskSave">确 定</el-button>
        <el-button @click="checkTaskCancel">取 消</el-button>
      </div>
    </el-dialog>

    <!--  审核撤回弹出框  -->
    <el-dialog :title="title" :visible.sync="backCheckTaskDialog" width="400px" append-to-body>
      <el-form ref="checkTaskForm" :model="backCheckTaskForm">
        <el-form-item>
          <el-input type="hidden" v-model="backCheckTaskForm.taskLogId"/>
        </el-form-item>
        <el-form-item label="撤回理由">
          <el-input type="textarea" v-model="backCheckTaskForm.msg"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="backCheckTaskSave">确 定</el-button>
        <el-button @click="backCheckTaskCancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listOrder, exportOrder, checkTaskOrder, backCheckTaskOrder } from "@/api/business/order";

export default {
  name: "Order",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 任务订单表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      checkTaskDialog: false,
      checkTaskForm: {},
      backCheckTaskDialog: false,
      backCheckTaskForm: {},
      // 订单状态字典
      statusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        taskId: null,
        taskName: null,
        customerId: null,
        submitContext: null,
        status: null,
      },
      orderStatusCss: '',
    };
  },
  created() {
    this.getList();
    this.getDicts("order_status").then(response => {
      this.statusOptions = response.data;
    });
  },
  methods: {
    /** 查询任务订单列表 */
    getList() {
      this.loading = true;
      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 订单状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        taskId: null,
        taskName: null,
        customerId: null,
        submitContext: null,
        submitImagePath: null,
        price: null,
        status: 0,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有任务订单数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportOrder(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    },

    /* 直接审核*/
    checkTask(taskLogId){
      this.title = "审核任务订单"
      this.checkTaskDialog = true;
      this.checkTaskForm.taskLogId = taskLogId;
    },
    /* 直接审核结果保存*/
    checkTaskSave(){
      if (this.checkTaskForm.status === undefined){
        this.msgError("请勾选审核结果")
        return false;
      }
      if (this.checkTaskForm.status === '3'){
        if (this.checkTaskForm.msg === undefined || this.checkTaskForm.msg === ''){
          this.msgError("请填写不通过理由")
          return false;
        }

      }
      checkTaskOrder(this.checkTaskForm).then(res => {
        if(res.code === 200){
          this.getList();
          this.msgSuccess(res.msg);
          this.checkTaskDialog = false;

        }else {
          this.msgError(res.msg)
        }
      })
    },
    checkTaskCancel(){
      this.checkTaskDialog = false;
    },

    backCheckTask(taskLogId){
      this.title = "撤回审核通过任务订单"
      this.backCheckTaskDialog = true;
      this.backCheckTaskForm.taskLogId = taskLogId;
    },
    backCheckTaskSave(){
      if (this.backCheckTaskForm.msg === undefined || this.backCheckTaskForm.msg === ""){
        this.msgError("请填写撤回理由")
        return false;
      }
      backCheckTaskOrder(this.backCheckTaskForm).then(res => {
        if(res.code === 200){
          this.getList();
          this.msgSuccess(res.msg);
          this.backCheckTaskDialog   = false;

        }else {
          this.msgError(res.msg)
        }
      })
    },
    backCheckTaskCancel(){
      this.backCheckTaskDialog = false;
    },
    /*订单颜色处理*/
    orderStatusCssMethod(status){
      switch (status){
        case 0:
          return 'order-status-css-blue';
        case 1:
          return 'order-status-css-blue';
        case 2:
          return 'order-status-css-green';
        case 3:
          return 'order-status-css-red';
        case 4:
          return 'order-status-css-orangered';
        default:
          return 'order-status-css-blue';
      }
    },
  }
};
</script>
<style>
    .order-status-css-green{
      color: green;
    }
    .order-status-css-red{
      color: red;
    }
    .order-status-css-blue{
      color: blue;
    }
    .order-status-css-orangered{
      color: orangered;
    }
    .check-task-msg{
      display: none;
    }
</style>
